# 测试新闻API功能
Write-Host "=== 测试新闻API功能 ===" -ForegroundColor Cyan

# 设置基础URL
$BASE_URL = "http://localhost:8900/api"

# 1. 获取新闻列表（初始应该为空）
Write-Host "\n1. 获取新闻列表..." -ForegroundColor Green
try {
    $newsList = Invoke-RestMethod -Uri "$BASE_URL/news" -Method Get -ContentType "application/json"
    Write-Host "✓ 获取新闻列表成功:" -ForegroundColor Green
    Write-Host (ConvertTo-Json $newsList -Depth 5)
} catch {
    Write-Host "✗ 获取新闻列表失败: $_" -ForegroundColor Red
    exit 1
}

# 2. 创建新闻
Write-Host "\n2. 创建新闻..." -ForegroundColor Green
try {
    $newNews = @{
        title = "测试新闻标题"
        content = "这是一条测试新闻内容"
        author = "测试作者"
    } | ConvertTo-Json
    
    $createdNews = Invoke-RestMethod -Uri "$BASE_URL/news" -Method Post -Body $newNews -ContentType "application/json"
    Write-Host "✓ 创建新闻成功:" -ForegroundColor Green
    Write-Host (ConvertTo-Json $createdNews -Depth 5)
    
    # 保存新闻ID用于后续测试
    $newsId = $createdNews.data.id
} catch {
    Write-Host "✗ 创建新闻失败: $_" -ForegroundColor Red
    exit 1
}

# 3. 获取新闻详情
Write-Host "\n3. 获取新闻详情..." -ForegroundColor Green
try {
    $newsDetail = Invoke-RestMethod -Uri "$BASE_URL/news/$newsId" -Method Get -ContentType "application/json"
    Write-Host "✓ 获取新闻详情成功:" -ForegroundColor Green
    Write-Host (ConvertTo-Json $newsDetail -Depth 5)
} catch {
    Write-Host "✗ 获取新闻详情失败: $_" -ForegroundColor Red
    exit 1
}

# 4. 更新新闻
Write-Host "\n4. 更新新闻..." -ForegroundColor Green
try {
    $updatedNews = @{
        title = "更新后的新闻标题"
        content = "这是更新后的新闻内容"
        author = "更新后的作者"
    } | ConvertTo-Json
    
    $result = Invoke-RestMethod -Uri "$BASE_URL/news/$newsId" -Method Put -Body $updatedNews -ContentType "application/json"
    Write-Host "✓ 更新新闻成功:" -ForegroundColor Green
    Write-Host (ConvertTo-Json $result -Depth 5)
} catch {
    Write-Host "✗ 更新新闻失败: $_" -ForegroundColor Red
    exit 1
}

# 5. 再次获取新闻列表（应该有一条新闻）
Write-Host "\n5. 再次获取新闻列表..." -ForegroundColor Green
try {
    $newsList = Invoke-RestMethod -Uri "$BASE_URL/news" -Method Get -ContentType "application/json"
    Write-Host "✓ 获取新闻列表成功:" -ForegroundColor Green
    Write-Host (ConvertTo-Json $newsList -Depth 5)
} catch {
    Write-Host "✗ 获取新闻列表失败: $_" -ForegroundColor Red
    exit 1
}

# 6. 删除新闻
Write-Host "\n6. 删除新闻..." -ForegroundColor Green
try {
    $deleteResult = Invoke-RestMethod -Uri "$BASE_URL/news/$newsId" -Method Delete -ContentType "application/json"
    Write-Host "✓ 删除新闻成功:" -ForegroundColor Green
    Write-Host (ConvertTo-Json $deleteResult -Depth 5)
} catch {
    Write-Host "✗ 删除新闻失败: $_" -ForegroundColor Red
    exit 1
}

# 7. 验证删除结果
Write-Host "\n7. 验证删除结果..." -ForegroundColor Green
try {
    $response = Invoke-RestMethod -Uri "$BASE_URL/news/$newsId" -Method Get -ContentType "application/json"
    Write-Host "✗ 删除验证失败: 新闻仍然存在" -ForegroundColor Red
    exit 1
} catch {
    # 期望得到404错误
    Write-Host "✓ 删除验证成功: 新闻不存在（404）" -ForegroundColor Green
}

# 8. 测试参数验证失败
Write-Host "\n8. 测试参数验证失败..." -ForegroundColor Green
try {
    $invalidNews = @{
        title = ""
        content = "缺少标题的新闻"
        author = "测试作者"
    } | ConvertTo-Json
    
    $response = Invoke-RestMethod -Uri "$BASE_URL/news" -Method Post -Body $invalidNews -ContentType "application/json"
    Write-Host "✗ 参数验证测试失败: 应该返回错误但没有" -ForegroundColor Red
    exit 1
} catch {
    Write-Host "✓ 参数验证测试成功: 正确返回了验证错误" -ForegroundColor Green
}

Write-Host "\n=== 所有新闻API测试完成！===" -ForegroundColor Green